#write by hashaki
# first edit on 20181121
# last change on 20181122
# vnpy那一套事件引擎我不会用，所以我自己写一套我能理解的事件引擎
import threading
import time
from queue import Queue

class HaEngine:
    def __init__(self):
        self.queue_data=Queue()
        self.queue_trade=Queue()
        self.activate=False
        self.thread_data=threading.Thread(target=self.run)
        self.thread_trade=threading.Thread(target=self.trade)
        self.start()
    
    def start(self):
        '''开始事件驱动'''
        self.activate=True
        self.thread_data.start()
        self.thread_trade.start()
    
    def stop(self):
        ''''停止事件驱动'''
        self.activate=False
        self.thread_trade.join()
        self.thread_data.join()

    def register_data(self,event):
        ''''把东西加进事件队列里'''
        self.queue_data.put(event)
    
    def register_trade(self,event):
        '''交易的order'''
        self.queue_trade.put(event)

    def run(self):
        '''跑起来'''
        while self.activate:
            event=self.queue_data.get()
            print(event.name)
            event.function

    def trade(self):
        ''''交易函数'''
        while self.activate:
            event=self.queue_trade.get()
            print(event.name)
            event.function